﻿using System;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;

namespace Ebt.Mvvm
{
    /// <summary>
    /// Allows to begin an animation when an event occurs using <see cref="EventBinding"/>.
    /// </summary>
    public class TriggerCommand : DependencyObject, ICommand
    {
        private event EventHandler CanExecuteChanged;

        public static readonly DependencyProperty StoryboardProperty =
            DependencyProperty.Register("Storyboard", typeof(Storyboard), typeof(TriggerCommand), new PropertyMetadata(default(Storyboard)));

        /// <summary>
        /// The <see cref="Storyboard"/> to begin in case if this command is executed.
        /// </summary>
        public Storyboard Storyboard
        {
            get { return (Storyboard) GetValue(StoryboardProperty); }
            set { SetValue(StoryboardProperty, value); }
        }

        #region ICommand

        event EventHandler ICommand.CanExecuteChanged
        {
            add
            {
                lock (CanExecuteChanged)
                {
                    CanExecuteChanged += value;
                }
            }
            remove
            {
                lock (CanExecuteChanged)
                {
                    CanExecuteChanged -= value;
                }
            }
        }

        bool ICommand.CanExecute(object parameter)
        {
            return true;
        }

        void ICommand.Execute(object parameter)
        {
            Storyboard.Stop();
            Storyboard.Begin();
        }

        #endregion
    }
}
